package com.emns.service.shijie;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emns.dao.ShijieCommentMapper;
import com.emns.pojo.ShijieComment;
import common.DateUtils;
import common.PageInfo;

@Service
public class ShijieCommentService {

	@Autowired
	ShijieCommentMapper sjcm;

	public PageInfo<ShijieComment> queryAllCommentById(int pageNo, int id, String content, String author,
			String startDate, String endDate) {
		Date sDate = null;
		Date eDate = null;
		if (content != null && !"".equals(content))
			content = "%" + content + "%";
		else
			content = null;
		if (author != null && !"".equals(author))
			author = "%" + author + "%";
		else
			author = null;
		if ((startDate != null && !"".equals(startDate)) && (endDate != null && !"".equals(endDate))) {
			startDate = startDate + " 00:00:00";
			sDate = DateUtils.parseDate(startDate, "yyyy-MM-dd HH:mm:ss");
			endDate = endDate + " 23:59:59";
			eDate = DateUtils.parseDate(endDate, "yyyy-MM-dd HH:mm:ss");
		} else {
			startDate = null;
			endDate = null;
		}
		PageInfo<ShijieComment> page = new PageInfo<ShijieComment>();
		page.setPageNum(pageNo);
		page.setPageCount(sjcm.getRecordSize(id, content, author, sDate, eDate));
		if (pageNo > page.getPageTotal()) {
			pageNo = page.getPageTotal();
			if (pageNo == 0)
				pageNo = 1;
		}
		int startIndex = (pageNo - 1) * page.getPageSize();
		page.setDataArray(sjcm.getRecords(id, content, author, sDate, eDate, startIndex, page.getPageSize()));
		return page;
	}

	public void delete(Integer[] ids) {
		for (Integer id : ids) {
			if (id != null) {
				ShijieComment comment = new ShijieComment();
				comment.setCommentId(id);
				comment.setState(2);
				sjcm.updateByPrimaryKeySelective(comment);
			}
		}
	}

	public void audit(Integer[] ids, boolean pass) {
		for (Integer id : ids) {
			if (id != null) {
				ShijieComment comment = new ShijieComment();
				comment.setCommentId(id);
				if (pass)
					comment.setState(1);
				else
					comment.setState(0);
				sjcm.updateByPrimaryKeySelective(comment);
			}
		}
	}

}
